<!DOCTYPE html>
<html lang="ZH-cn">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Login</title>
  <link rel="stylesheet" href="../lib/layui/css/layui.css">
  <link rel="stylesheet" href="../css/login.css">
</head>

<body>
  <div class="kit-login">
    <div class="kit-login-bg"></div>
    <div class="kit-login-wapper">
      <h2 class="kit-login-slogan">欢迎使用 <br> KITADMIN 2.0 后台管理模板</h2>
      <div class="kit-login-form">
        <h4 class="kit-login-title">登录</h4>
        <form class="layui-form">
          <div class="kit-login-row">
            <div class="kit-login-col">
              <i class="layui-icon">&#xe612;</i>
              <span class="kit-login-input">
                            <input type="text" name="userName" lay-verify="required" placeholder="用户名" />
                        </span>
            </div>
            <div class="kit-login-col"></div>
          </div>
          <div class="kit-login-row">
            <div class="kit-login-col">
              <i class="layui-icon">&#xe64c;</i>
              <span class="kit-login-input">
                            <input type="password" name="password" lay-verify="required" placeholder="密码" />
                        </span>
            </div>
            <div class="kit-login-col"></div>
          </div>
          <div class="kit-login-row">
            <div class="kit-login-col">
              <input type="checkbox" name="rememberMe" value="true" title="记住帐号" lay-skin="primary">
            </div>
          </div>
          <div class="kit-login-row">
            <button class="layui-btn kit-login-btn" lay-submit="submit" lay-filter="login_hash">登录</button>
          </div>
          <div class="kit-login-row" style="margin-bottom:0;">
            <a href="javascript:;" style="color: rgb(153, 153, 153); text-decoration: none; font-size: 13px;" id="forgot">忘记密码</a>
          </div>
        </form>
      </div>
    </div>
  </div>
  <script src="../lib/polyfill.min.js"></script>
  <script src="../lib/layui/layui.js"></script>
  <script src="../lib/axios.js"></script>
  <script>
      layui.config({
          base: '../js/'
      }).use('cookie');
  </script>
  <script>
    //'axios', 'lodash'
    layui.use(['layer','form','cookie'], function() {
      var form = layui.form,
       // axios = layui.axios,
        $ = layui.jquery;
      $('#forgot').on('click', function() {
        layer.msg('请联系管理员.');
      });
       form.render();
      //监听提交
      form.on('submit(login_hash)', function(data) {
        axios.post('/api/user/login', data.field).then(function (response) {
              if(response.data.code==200){
                  //form.val("formTest",response.data.data)
                  layer.msg(response.data.data.msg,
                      {icon: 1,
                          time: 1000 }//2秒关闭（如果不配置，默认是3秒）
                      ,function () {
                          $.cookie('token',response.data.data.token, {  path: '/' })
                          $.cookie('user',JSON.stringify(response.data.data.user), {  path: '/' })
                          $.cookie('realName',response.data.data.user.realName, {  path: '/' })
                          location.href = '/';
                      })
              }else{
                  layer.msg(response.data.msg)
              }
          }) .catch(function (error) {
            layer.msg('系统异常！！');
              console.log(error);
          });
          return false;
      });
    });

    // function getParams(href) {
    //   var p = href.substr(href.indexOf('?') + 1);
    //   if (href === p) return null;
    //   var params = p.split('&');
    //   var data = {};
    //   _.forEach(params, function(item, index) {
    //     var kv = item.split('=');
    //     var key = kv[0];
    //     var value = kv[1];
    //     data[key] = value;
    //   });
    //   return data;
    // }
  </script>
</body>

</html>